System.Gadget.onSettingsClosing = SettingsClosing;
function SettingsClosing(event){
	var failed = false;
	if (event.closeAction == event.Action.commit){
		try {
			System.Gadget.Settings.writeString('TitleColor', document.getElementById('TitleColor').value);
			System.Gadget.Settings.writeString('TitleTextColor', document.getElementById('TitleTextColor').value);
			System.Gadget.Settings.writeString('BuddyColor', document.getElementById('BuddyColor').value);
			System.Gadget.Settings.writeString('BuddyAltColor', document.getElementById('BuddyAltColor').value);
			System.Gadget.Settings.writeString('BuddyTextColor', document.getElementById('BuddyTextColor').value);
			System.Gadget.Settings.writeString('BuddyIdleTextColor', document.getElementById('BuddyIdleTextColor').value);
			System.Gadget.Settings.writeString('BuddyHighlightColor', document.getElementById('BuddyHighlightColor').value);
			System.Gadget.Settings.writeString('BuddyHighlightTextColor', document.getElementById('BuddyHighlightTextColor').value);
			System.Gadget.Settings.writeString('BuddyHighlightIdleTextColor', document.getElementById('BuddyHighlightIdleTextColor').value);
			
			if(document.getElementById('SizeDynamic').checked){
				System.Gadget.Settings.write('SizeType', 0);
			} else {
				System.Gadget.Settings.write('SizeType', 1);
			}
			var sizeStaticBuddies = parseInt(document.getElementById('SizeStaticBuddies').value);
			if(isNaN(sizeStaticBuddies) || sizeStaticBuddies < 1){
				sizeStaticBuddies = 5;
			}
			System.Gadget.Settings.write('SizeStaticBuddies', sizeStaticBuddies);
			
			if(document.getElementById('CustomPathBox').checked){
				System.Gadget.Settings.writeString('CustomPath', document.getElementById('CustomPath').value);
			} else {
				System.Gadget.Settings.writeString('CustomPath', '');
			}
		} catch(e) {
			// do something
		}
	}
	event.cancel = failed;
}
function readSettings(){
	var titleColor = System.Gadget.Settings.readString('TitleColor');
	var titleTextColor = System.Gadget.Settings.readString('TitleTextColor');
	var buddyColor = System.Gadget.Settings.readString('BuddyColor');
	var buddyAltColor = System.Gadget.Settings.readString('BuddyAltColor');
	var buddyTextColor = System.Gadget.Settings.readString('BuddyTextColor');
	var buddyIdleTextColor = System.Gadget.Settings.readString('BuddyIdleTextColor');
	var buddyHighlightColor = System.Gadget.Settings.readString('BuddyHighlightColor');
	var buddyHighlightTextColor = System.Gadget.Settings.readString('BuddyHighlightTextColor');
	var buddyHighlightIdleTextColor = System.Gadget.Settings.readString('BuddyHighlightIdleTextColor');
	
	document.getElementById('TitleColor').value = titleColor;
	document.getElementById('TitleTextColor').value = titleTextColor;
	document.getElementById('BuddyColor').value = buddyColor;
	document.getElementById('BuddyAltColor').value = buddyAltColor;
	document.getElementById('BuddyTextColor').value = buddyTextColor;
	document.getElementById('BuddyIdleTextColor').value = buddyIdleTextColor;
	document.getElementById('BuddyHighlightColor').value = buddyHighlightColor;
	document.getElementById('BuddyHighlightTextColor').value = buddyHighlightTextColor;
	document.getElementById('BuddyHighlightIdleTextColor').value = buddyHighlightIdleTextColor;
	
	var sizeType = System.Gadget.Settings.read('SizeType');
	var sizeStaticBuddies = System.Gadget.Settings.read('SizeStaticBuddies');
	
	sizeSelected(sizeType);
	document.getElementById('SizeStaticBuddies').value = sizeStaticBuddies;
	
	var customPath = System.Gadget.Settings.readString('CustomPath');
	if(customPath != ''){
		document.getElementById('CustomPath').value = customPath;
		document.getElementById('CustomPathBox').checked = true;
		customPathClick();
	} else {
		document.getElementById('CustomPath').disabled = true;
	}
}
function sizeSelected(which){
	if(which == 0){
		document.getElementById('SizeDynamic').checked = true;
		document.getElementById('SizeStatic').checked = false;
		document.getElementById('SizeStaticBuddies').disabled = true;
	} else {
		document.getElementById('SizeDynamic').checked = false;
		document.getElementById('SizeStatic').checked = true;
		document.getElementById('SizeStaticBuddies').disabled = false;
	}
}
function customPathClick() {
	if(document.getElementById('CustomPathBox').checked){
		document.getElementById('CustomPath').disabled = false;
	} else {
		document.getElementById('CustomPath').disabled = true;
	}
}
function colorPreset(which){
	switch(which){
	case 0:
		document.getElementById('TitleColor').value = '#11131D';
		document.getElementById('TitleTextColor').value = '#8888AA';
		document.getElementById('BuddyColor').value = '#21293A';
		document.getElementById('BuddyAltColor').value = '#151A27';
		document.getElementById('BuddyHighlightColor').value = '#31397A';
		document.getElementById('BuddyTextColor').value = '#FFFFFF';
		document.getElementById('BuddyHighlightTextColor').value = '#FFFFFF';
		document.getElementById('BuddyIdleTextColor').value = '#666699';
		document.getElementById('BuddyHighlightIdleTextColor').value = '#000033';
		break;
	case 1:
		document.getElementById('TitleColor').value = '#111D13';
		document.getElementById('TitleTextColor').value = '#88AA88';
		document.getElementById('BuddyColor').value = '#213A29';
		document.getElementById('BuddyAltColor').value = '#15271A';
		document.getElementById('BuddyHighlightColor').value = '#317A39';
		document.getElementById('BuddyTextColor').value = '#FFFFFF';
		document.getElementById('BuddyHighlightTextColor').value = '#FFFFFF';
		document.getElementById('BuddyIdleTextColor').value = '#669966';
		document.getElementById('BuddyHighlightIdleTextColor').value = '#003300';
		break;
	case 2:
		document.getElementById('TitleColor').value = '#2D0301';
		document.getElementById('TitleTextColor').value = '#BA7878';
		document.getElementById('BuddyColor').value = '#4A1911';
		document.getElementById('BuddyAltColor').value = '#370A05';
		document.getElementById('BuddyHighlightColor').value = '#8A2921';
		document.getElementById('BuddyTextColor').value = '#FFFFFF';
		document.getElementById('BuddyHighlightTextColor').value = '#FFFFFF';
		document.getElementById('BuddyIdleTextColor').value = '#996666';
		document.getElementById('BuddyHighlightIdleTextColor').value = '#330000';
		break;
	case 3:
		document.getElementById('TitleColor').value = '#000000';
		document.getElementById('TitleTextColor').value = '#FFFF00';
		document.getElementById('BuddyColor').value = '#FFFF99';
		document.getElementById('BuddyAltColor').value = '#EEEE88';
		document.getElementById('BuddyHighlightColor').value = '#000000';
		document.getElementById('BuddyTextColor').value = '#000000';
		document.getElementById('BuddyHighlightTextColor').value = '#FFFFFF';
		document.getElementById('BuddyIdleTextColor').value = '#999999';
		document.getElementById('BuddyHighlightIdleTextColor').value = '#CCCCCC';
		break;
	case 4:
		document.getElementById('TitleColor').value = '#DDDDDD';
		document.getElementById('TitleTextColor').value = '#444444';
		document.getElementById('BuddyColor').value = '#FFFFFF';
		document.getElementById('BuddyAltColor').value = '#EEEEEE';
		document.getElementById('BuddyHighlightColor').value = '#6666FF';
		document.getElementById('BuddyTextColor').value = '#000000';
		document.getElementById('BuddyHighlightTextColor').value = '#FFFFFF';
		document.getElementById('BuddyIdleTextColor').value = '#999999';
		document.getElementById('BuddyHighlightIdleTextColor').value = '#CCCCFF';
		break;
	}
}